package cn.e2win.biz.activity;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.base.json.vo.WeixinUser;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.comm.WeiXinController;
import cn.e2win.biz.dbo.Activity;
import cn.e2win.biz.dbo.CyzjOrg;
import cn.e2win.biz.util.StringUtils;
import net.sf.json.JSONObject;


@Controller
@Scope("request")
@RequestMapping("/m")
public class ActivityControllerM extends BaseController {

	@Autowired
	private ActivityService activityService;
	@Autowired
	private CommonService commonService;

	@Override
	public BaseService initService() {
		return activityService;
	}

	@RequestMapping("activityMain")
	public String activityMain(HttpServletRequest request,@ModelAttribute Activity activity, ModelMap modelMap){
		pager.getScMap().put("approveStatus", "APPROVED");

		if(!StringUtils.isEmpty(activity.getOrgId())){
			pager.getScMap().put("orgId",activity.getOrgId()+"");
		}
		if(!StringUtils.isEmpty(activity.getActivityType())){
			pager.getScMap().put("activityType",activity.getActivityType());
		}
		
		
		activityService.pagerActivity(pager);
		modelMap.put("pager", pager);

		List<SysLabel> labelMainList = commonService.getSysLabelList(CyzjOrg.class.getSimpleName(),"labelMain");
		modelMap.addAttribute("labelMainList",labelMainList);


		modelMap.addAttribute("orgList",commonService.getOrgList("Cyzj",new HashMap()));
		return "m/activityMain";
	}

	@RequestMapping("activityByUser")
	public String activityByUser(HttpServletRequest request/*, @PageParam Pager pager*/, ModelMap modelMap, HttpSession httpSession){
//		pager.getScMap().put("approveStatus", "SUBMITED");
//		activityService.pagerActivity(pager);
//		modelMap.put("activityPager", pager);
		SysUser sysUser = (SysUser) httpSession.getAttribute("user");
		if(sysUser==null || StringUtils.isEmpty(sysUser.getUserType())){
			return "redirect:/m/login.do";
		}

		if("CyzjEmp".equals(sysUser.getUserType())){
			//举办中
			Pager nowPager = new Pager();
			nowPager.setOrderField("beginTime");
			nowPager.setOrderDirection("DESC");
			nowPager.getScMap().put("approveStatus", "APPROVED");
			nowPager.getScMap().put("beginTime_B", new Date()+"");
			nowPager.getScMap().put("endTime_E", new Date()+"");
			nowPager.getScMap().put("user_I","addBy");
			nowPager.setCurrUser(pager.getCurrUser());
			activityService.pagerActivity(nowPager);
			modelMap.put("nowPager", nowPager);

			//待审核
			Pager approvePager = new Pager();
			approvePager.setOrderField("beginTime");
			approvePager.setOrderDirection("DESC");
			approvePager.getScMap().put("approveStatus", "SUBMITED");
			approvePager.getScMap().put("user_I","addBy");
			approvePager.setCurrUser(pager.getCurrUser());
			activityService.pagerActivity(approvePager);
			modelMap.put("approvePager", approvePager);

			return "m/cyzjActivity";
		}else if("ZdzxEmp".equals(sysUser.getUserType())){
			//待审核
			Pager approvePager = new Pager();
			approvePager.setOrderField("beginTime");
			approvePager.setOrderDirection("DESC");
			approvePager.getScMap().put("approveStatus", "SUBMITED");
			approvePager.getScMap().put("user_I","admin");
			approvePager.setCurrUser(pager.getCurrUser());
			activityService.pagerActivity(approvePager);
			modelMap.put("approvePager", approvePager);

			//举办中
			Pager nowPager = new Pager();
			nowPager.setOrderField("beginTime");
			nowPager.setOrderDirection("DESC");
			nowPager.getScMap().put("approveStatus", "APPROVED");
			nowPager.getScMap().put("beginTime_B", new Date()+"");
			nowPager.getScMap().put("endTime_E", new Date()+"");
			nowPager.getScMap().put("user_I","admin");
			nowPager.setCurrUser(pager.getCurrUser());
			activityService.pagerActivity(nowPager);
			modelMap.put("nowPager", nowPager);

			return "m/zdzxActivity";
		}else if("DsEmp".equals(sysUser.getUserType())){
			//待参加
			Pager waitPager = new Pager();
			waitPager.setOrderField("beginTime");
			waitPager.setOrderDirection("DESC");
			waitPager.getScMap().put("beginTime_B",new Date()+"");
			waitPager.getScMap().put("approveStatus", "APPROVED");
			waitPager.getScMap().put("user_I","join");
			waitPager.setCurrUser(pager.getCurrUser());
			activityService.pagerActivity(waitPager);
			modelMap.put("waitPager", waitPager);
			//已参加
			Pager completePager = new Pager();
			completePager.setOrderField("beginTime");
			completePager.setOrderDirection("DESC");
			completePager.getScMap().put("beginTime_E",new Date()+"");
			completePager.getScMap().put("approveStatus", "APPROVED");
			completePager.getScMap().put("user_I","join");
			completePager.setCurrUser(pager.getCurrUser());
			activityService.pagerActivity(completePager);
			modelMap.put("completePager", completePager);
			return "m/dsActivity";
		}else{
			return "redirect:/m/login.do";
		}
		
	}

	@RequestMapping("activityPager")
	@ResponseBody
	public Pager activityPager(HttpServletRequest request, ModelMap modelMap){
		if(StringUtils.isEmpty(pager.getOrderDirection())){
			pager.setOrderField("beginTime");
			pager.setOrderDirection("DESC");
		}
		if(StringUtils.isEmpty(pager.getScMap().get("approveStatus"))) {
			pager.getScMap().put("approveStatus", "APPROVED");
		}
		activityService.pagerActivity(pager);
		return pager;
	}

	@RequestMapping("/activityDetail")
	public String activityDetail(HttpServletRequest request, @RequestParam("id")Integer id,
							 ModelMap modelMap) {
		Map<String, String> param = new HashMap<String, String>();
		WeixinUser weixinUser =(WeixinUser)request.getSession().getAttribute("weixinUser");
		if(weixinUser!=null){
			param.put("x_I", weixinUser.getBaiduX());
			param.put("y_I", weixinUser.getBaiduY());
		}
		modelMap.addAttribute("activity",activityService.getActivityOrgDetail(id, param));
		return "/m/activityDetail";
	}

	@RequestMapping("/approveActivity")
	@ResponseBody
	public Result approveActivity(@RequestParam("id")Integer id,@RequestParam("approveStatus")String approveStatus){
		Result result = new Result();
		try {
			Activity activity = (Activity) activityService.getEntity(Activity.class,id);
			Integer adminId = activity.getAdminUserId();
			Integer adminId2 = activity.getAdminUserId2();
			if(pager.getCurrUser()==null){
				throw new MyException("您没有权限审核该活动");
			}
			Integer currUserId = pager.getCurrUser().getId();
			if(currUserId.equals(adminId) || currUserId.equals(adminId2)){
                activity.setApproveStatus(approveStatus);
                activityService.edit(activity);
            }else{

                throw new MyException("您没有权限审核该活动");
            }
		} catch (MyException e) {
			result.setStatusCode(Result.FAILED);
			result.setMessage(e.getMsg());
		}
		return result;
	}


}
